﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace WebPresentation
{
    public partial class Department : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("LoginPage.aspx");
                }
                if (!Session["Role"].Equals("Admin"))
                {
                    Response.Redirect("DocumentUser.aspx");
                }
            }       
        }
        protected void ImageButtonadd_Click(object sender, ImageClickEventArgs e)
        {
            DepartmentBO DepBo = new DepartmentBO();
            string DepName = txtdepname.Text;
            string DepCode = txtdepcode.Text;
            if (DepBo.InsertDepartment(DepName, DepCode) > 0)
            {
                DepDataSource = new ObjectDataSource();
                dtgdepartment.DataBind();
                Label10.Text = "Add successfully!";
            }
            else
            {
                Label10.Text = "Add failed.";
            }
        }

        protected void dtgdepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            DepartmentBO DepBo = new DepartmentBO();
            DataTable dt = (DataTable)DepBo.GetByID(Int32.Parse(dtgdepartment.SelectedRow.Cells[1].Text));
            txtNameEdit.Text = dt.Rows[0]["DepName"].ToString();
            txtCodeEdit.Text = dt.Rows[0]["DepCode"].ToString();
        }

        protected void LinkButtondel_Click(object sender, EventArgs e)
        {
            if (dtgdepartment.SelectedRow != null)
            {
                DepartmentBO DepBo = new DepartmentBO();
                try
                {
                    if (DepBo.DeleteDepartment(Int32.Parse(dtgdepartment.SelectedRow.Cells[1].Text)) > 0)
                    {
                        Label10.Text = "Delete successfully";
                        dtgdepartment.DataBind();
                    }
                    else
                    {
                        Label10.Text = "Delete failed!";
                    }
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", "<script> alert('Warning: Please delete all the data that refer to this record first!')</script>");
                }
            }
        }

        protected void LinkButtonedit_Click(object sender, EventArgs e)
        {
            if (dtgdepartment.SelectedRow != null)
            {
                DepartmentBO DepBo = new DepartmentBO();
                DataTable dt = (DataTable)DepBo.GetByID(Int32.Parse(dtgdepartment.SelectedRow.Cells[1].Text));
                txtNameEdit.Text = dt.Rows[0]["DepName"].ToString();
                txtCodeEdit.Text = dt.Rows[0]["DepCode"].ToString();
            }
        }

        protected void ImageButtonedit_Click(object sender, ImageClickEventArgs e)
        {
            if (dtgdepartment.SelectedRow != null)
            {
                DepartmentBO DepBo = new DepartmentBO();
                string DepName = txtNameEdit.Text;
                string DepCode = txtCodeEdit.Text;
                int depid = Int32.Parse(dtgdepartment.SelectedRow.Cells[1].Text);

                if (DepBo.UpdateDepartment(DepName, DepCode, depid) > 0)
                {
                    DepDataSource = new ObjectDataSource();
                    Label10.Text = "Edit Successfully!";
                    dtgdepartment.DataBind();
                }
                else
                {
                    Label10.Text = "Edit failed!";
                }
            }
        }

    }
}